using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using OpenMVCRM.Entities;

namespace OpenMVCRM.DataAccess
{
    class AddressDA
    {
        readonly static string connString = ConfigurationManager.ConnectionStrings["CRMContext"].ConnectionString;
        public static OperationStatus SaveAddress(Address address)
        {
            var opStatus = new OperationStatus();

            using (var conn = new SqlConnection(connString))
            {
                using (var cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "uspSaveAddress";
                    cmd.Parameters.AddWithValue("@addressId", address.AddressID);
                    cmd.Parameters.AddWithValue("@addressLine1", address.AddressLine1);
                    cmd.Parameters.AddWithValue("@addressLine2", (object)address.AddressLine2 ?? DBNull.Value);
                    cmd.Parameters.AddWithValue("@city", (object)address.City ?? DBNull.Value);
                    cmd.Parameters.AddWithValue("@countryID", (object)address.CountryID ?? DBNull.Value);
                    cmd.Parameters.AddWithValue("@stateId", (object)address.StateID ?? DBNull.Value);
                    cmd.Parameters.AddWithValue("@zipCode", (object)address.ZipCode ?? DBNull.Value);
                    cmd.Parameters.AddWithValue("@ownerId", address.OwnerID);


                    DbParameter returnValue;
                    returnValue = cmd.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    cmd.Parameters.Add(returnValue);

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    int result = Convert.ToInt32(returnValue.Value);
                    conn.Close();
                    opStatus.OperationID = result;
                    opStatus.Status = true;
                }
            }

            return opStatus;
        }

    }
}
